package com.mida.bms.report.controller;

import com.mida.bms.report.common.cons.CharacterConst;
import com.mida.bms.report.entity.DocStructReport;
import com.mida.bms.report.process.DocReportPdfProcess;
import com.mida.bms.report.service.DocReportCriticalService;
import com.mida.bms.report.service.DocReportPdfService;
import com.mida.bms.report.service.DocReportService;
import com.mida.bms.report.vo.*;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <h1>报告书写</h1>
 * <p>
 *
 * </p>
 *
 * @author jian.li on 2021/4/27 16:37
 */
@RestController
@RequestMapping("/docReport")
@CrossOrigin
@Slf4j
public class DocReportController {

    @Autowired
    private DocReportService docReportService;

    @Autowired
    private DocReportCriticalService docReportCriticalService;

    @Autowired
    private DocReportPdfService docReportpdfService;

    @Autowired
    private DocReportPdfProcess docReportPdfProcess;

    /**
     * 1.暂存报告
     *
     * @param request 暂存报告请求参数
     * @return
     */
    @PostMapping(value = "/saveTeporarily",consumes = {"application/json"})
    @ResponseBody
    public ListResult<ReportResult> saveTemporarily(@RequestBody DocReportPreservationRequest request) {
        docReportService.requestCheck(request);
        return docReportService.saveTemporarily(request);
    }

    /**
     * 2.签署报告
     * <p>
     * 共保存三份:
     * 1.json
     * 2.pdf
     * 3.结构化报告
     *
     * @param request 保存报告请求参数
     * @return
     */
    @PostMapping(value = "/saveDocReport",consumes = {"application/json"})
    @ResponseBody
    public ListResult<String>  saveDocReport(@RequestBody DocReportPreservationRequest request) {
        return docReportService.saveDocReport(request);
    }

    /**
     * 3.报告(初写完成待审核)编辑/撤回
     *
     * @param stReportSoid 报告存储信息标识
     * @return
     */
    @PostMapping(value = "/editDocReport",consumes = {"application/json"})
    @ResponseBody
    public ActionResult editDocReport(@RequestParam("stReportSoid") Long stReportSoid,@RequestParam("employeeSoid") Long employeeSoid) {
        return docReportService.editDocReport(stReportSoid,employeeSoid);
    }

    /**
     * 4.报告回显
     *
     * @param request 报告回显请求参数
     * @return
     */
    @PostMapping(value = "/getDocReport",consumes = {"application/json"})
    @ResponseBody
    public ListResult<ReportResult> getDocReport(@RequestBody DocReportInitializationRequest request) {
        return docReportService.getDocReport(request);
    }

    /**
     * 5.业务态保存报告模板
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/saveTemplate",consumes = {"application/json"})
    @ResponseBody
    public ActionResult saveTemplate(@RequestBody DocReportPreservationRequest request) {
        return docReportService.saveTemplate(request);
    }

    /**
     * 6.接收报告，将报告改为已审核，保存报告
     *
     * @param reportRequest
     * @return
     */
    @PostMapping(value = "/modifyReportStatus",consumes = {"application/json"})
    @ResponseBody
    public ActionResult modifyReportStatus(@RequestBody DocReportPreservationRequest reportRequest) {

        ActionResult result = new ActionResult();
        try {
            return docReportService.modifyReportStatus(reportRequest);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("审核报告失败");
            return result;
        }
    }

    /**
     * 7.退回报告，将报告改为审核已退回
     *
     * @param stReportSoid
     * @return
     */
    @GetMapping("/modifyStatus")
    @ResponseBody
    public ActionResult modifyStatus(@RequestParam("stReportSoid") String stReportSoid,@RequestParam("employeeSoid") Long employeeSoid) {
        return docReportService.modifyStatus(stReportSoid,employeeSoid);
    }

    /**
     * 8.报告通过审核，撤回报告
     *
     * @param
     * @return
     */
    @GetMapping(value = "/withdrawStatus",consumes = {"application/json"})
    @ResponseBody
    public ActionResult withdrawStatus(@RequestParam("stReportSoid") String stReportSoid, @RequestParam("employeeSoid") Long employeeSoid) {
        return docReportService.withdrawStatus(stReportSoid,employeeSoid);
    }

//    /**
//     * 8.报告通过审核，撤回报告
//     *
//     * @param
//     * @return
//     */
//    @PostMapping(value = "/withdrawStatuss",consumes = {"application/json"})
//    @ResponseBody
//    public ActionResult withdrawStatuss(@RequestBody DocReportPreservationRequest reportRequest) {
//        return docReportService.withdrawStatus(reportRequest);
//    }

    /**
     * 9.保存报告pdf文件
     *
     * @param stReportSoid 报告存储信息标识
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/savePdf",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ActionResult savePdf(@RequestParam("stReportSoid") Long stReportSoid, @RequestPart("file") MultipartFile file, @RequestParam("employeeSoid") Long employeeSoid) {
        return docReportpdfService.savePdf(stReportSoid, file, employeeSoid);
    }

    /**
     * 10.获取报告pdf
     *
     * @param request
     * @return
     */
    @PostMapping("/getReportPdf")
    @ResponseBody
    public void getReportPdf(@RequestBody ReportSearchRequest request, HttpServletResponse httpServletResponse){
        docReportpdfService.getReportPdf(request,httpServletResponse);
    }

    /**
     * 11.保存结构化报告
     * @param request
     * @return
     */
    @PostMapping(value = "/saveStructuredReport",consumes = {"application/json"})
    @ResponseBody
    public ActionResult saveStructuredReport(@RequestBody StructuredReportRequest request){
        return docReportService.saveStructuredReport(request);
    }

    /**
     * 13.根据登记号，检查号签署报告
     */
    @PostMapping(value = "/saveDocReportByRegistrationAndExam",consumes = {"application/json"})
    @ResponseBody
    public ListResult<String> saveDocReportByRegistrationAndExam(@RequestBody DocReportPreservationRequest request) {
        return docReportService.saveDocReport(request);
    }

    /**
     * 14.根据申请单标识，登记标识，检查号 报告回显
     *
     * @param request 报告回显请求参数
     * @return
     */
    @PostMapping(value = "/getDocReportByTypes",consumes = {"application/json"})
    @ResponseBody
    public ListResult<ReportResult> getDocReportByTypes(@RequestBody DocReportInitializationRequest request) {
        return docReportService.getDocReportByTypes(request);
    }

    /**
     * 15.根据申请单号，登记单号，检查号暂存报告
     *
     * @param request 暂存报告请求参数
     * @return
     */
    @PostMapping(value = "/saveTemporarilyTypes",consumes ={"application/json"} )
    @ResponseBody
    public ListResult<ReportResult> saveTemporarilyTypes(@RequestBody DocReportPreservationRequest request) {
        docReportService.requestCheck(request);
        return docReportService.saveTemporarily(request);
    }

    /**
     * 16.根据StReportSoid从docReportRequisitionRelation表中查询全部列表
     * @return
     */
    @PostMapping(value = "/selectByStReportSoid",consumes ={"application/json"} )
    @ResponseBody
    public List<DocReportRequisitionRelationVo> selectByStReportSoid(@RequestParam("stReportSoid") Long stReportSoid) {
        return docReportService.selectByStReportSoid(stReportSoid);
    }


    /**
     * 17.获取报告pdfurl
     *
     * @param request
     * @return
     */
    @PostMapping("/getReportPdfUrl")
    @ResponseBody
    public ListResult<String> getReportPdfUrl(@RequestBody ReportSearchRequest request){
       return docReportpdfService.getReportPdfUrl(request);
    }


    /**
     * 获取图片
     *
     * @param
     * @return
     */
    @GetMapping("/getReportPdfImage")
    @ResponseBody
    public List<String> getReportPdfImage(){
       return docReportpdfService.getReportPdfImage();
    }


    /**
     * 获取报告pdf历史(提供给上游申请单使用)
     *
     * @param requestSoidList
     * @return
     */
    @PostMapping("/getReportPdfHistory")
    @ResponseBody
    public ListResult<Map<String,List<String>>> getReportPdfHistory(@RequestBody List<Long> requestSoidList){
        return docReportpdfService.getReportPdfHistory(requestSoidList);
    }

    /**
     * 18.查询待书写的报告和已完成报告的数量
     * @return
     */
    @PostMapping(value = "/queryReportsNumbers",consumes ={"application/json"} )
    @ResponseBody
    public ListResult<StatusSum> queryReportsNumbers(@RequestBody QueryReportsNumberDto queryReportsNumberDto) {
        ListResult listResult = new ListResult();

        try {
            return docReportService.queryReportsNumbers(queryReportsNumberDto);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            listResult.setMessage("查询数量处理失败");
            listResult.setStatus(HttpStatus.BAD_REQUEST.value());
            return listResult;
        }

    }

    /**
     * 19.根据报告soid查询报告
     *
     * @param stReportSoid 报告soid
     * @return
     */
    @PostMapping(value = "/getDocReportByStReportSoid")
    @ResponseBody
    public ListResult<ReportResult> getDocReportByStReportSoid(@RequestParam("stReportSoid") Long stReportSoid) {
        return docReportService.getDocReportByStReportSoid(stReportSoid);
    }
    /**
     *  发布报告(支持批量)
     *
     * @param reportPublishRequest 发布报告请求参数
     * @return
     */
    @PostMapping(value = "/publishReport",consumes = {"application/json"})
    @ResponseBody
    public ActionResult publishReport(@RequestBody ReportPublishRequest reportPublishRequest){
        return docReportService.publishReport(reportPublishRequest);
    }

    /**
     * 召回报告
     *
     * @param reportRecallRequest 召回报告请求参数
     * @return
     */
    @PostMapping(value = "/recallReport",consumes = {"application/json"})
    @ResponseBody
    public ActionResult recallReport(@RequestBody ReportRecallRequest reportRecallRequest){
        return docReportService.recallReport(reportRecallRequest);
    }

    /**
     * 暂缓报告
     *
     * @param requestSoid 申请单id
     * @param personSoid 检查id
     * @param registrationFormSoid 登记id
     * @return
     */
    @PostMapping(value = "/postponeReport",consumes = {"application/json"})
    @ResponseBody
    public ActionResult postponeReport(@RequestParam("requestSoid") String requestSoid, @RequestParam("personSoid")String personSoid,
                                       @RequestParam("registrationFormSoid")String registrationFormSoid){
        return docReportService.postponeReport(requestSoid,personSoid,registrationFormSoid);
    }

    /**
     * 危急值接收(提供给外部代理调用)
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/recieveCriticalValue",consumes = {"application/json"})
    @ResponseBody
    public ListResult recieveCriticalValue(@RequestBody CriticalValueRecieveRequest request){
        return docReportCriticalService.recieveCriticalValue(request);
    }

    /**
     * 危急值反馈(提供给外部代理调用)
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/feedbackCriticalValue",consumes = {"application/json"})
    @ResponseBody
    public ListResult feedbackCriticalValue(@RequestBody CriticalValueFeedbackRequest request){
        return docReportCriticalService.feedbackCriticalValue(request);
    }

    /**
     * 修改危急值主表状态为已发送
     *
     * @param docReportCritivalValueSoid
     * @return
     */
    @GetMapping(value = "/sentCriticalValue")
    @ResponseBody
    public ActionResult sentCriticalValue(@RequestParam("docReportCritivalValueSoid") Long docReportCritivalValueSoid){
        return docReportCriticalService.sentCriticalValue(docReportCritivalValueSoid);
    }

    /**
     * 保存危急值并转发到代理(网关)
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/saveCritivalValue")
    @ResponseBody
    ListResult<Long> saveCritivalValue(@RequestBody CriticalValueRequest request){
        return docReportCriticalService.saveCritivalValue(request);
    }


    /**
     * 获取报告pdf
     *
     * @param request
     * @return
     */
    @PostMapping(value ="/gainReportPdf")
    @ResponseBody
    public void gainReportPdf(@RequestBody ReportSearchRequest request,
                              HttpServletResponse httpServletResponse){
        docReportPdfProcess.gainReportPdf(request,httpServletResponse);
    }


    /**
     * 获取报告pdfurl
     *
     * @param request
     * @return
     */
    @PostMapping(value ="/gainReportPdfByStReportSoid",
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    String gainReportPdfByStReportSoid(@RequestBody ReportSearchRequest request){
        try {
            return docReportPdfProcess.gainReportPdfByStReportSoid(request);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return CharacterConst.SpecialSymbol.EMPTY;
        }
    }

    /**
     * 获取报告
     * @param request
     * @return
     */
    @PostMapping(value = "/gainDocReportByStReportSoid",
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    DocStructReport gainDocReportByStReportSoid(@RequestBody ReportSearchRequest request) {
        try {
            return docReportPdfProcess.gainDocReportByStReportSoid(request);
        } catch (Exception ex){
            log.error(ex.getMessage(), ex);
            return new DocStructReport();
        }
    }
}
